package com.crazypos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crazypos.dao.BrandDao;
import com.crazypos.dao.OrganizationDao;
import com.crazypos.enums.YesOrNo;
import com.crazypos.exceptions.MyException;
import com.crazypos.pojo.BrandEntity;
import com.crazypos.pojo.bo.CreateBrandBO;
import com.crazypos.pojo.bo.UpdateBrandBO;
import com.crazypos.service.IBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BrandService extends ServiceImpl<BrandDao, BrandEntity> implements IBrandService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private OrganizationDao organizationDao;

    @Override
    @Transactional
    public void createBrand(boolean isAdmin, CreateBrandBO bo, String userId, String organizationId) {
        BrandEntity brandEntity = new BrandEntity();
        if (isAdmin) {
            brandEntity.setOrganizationId("0");
            brandEntity.setIsSystem(YesOrNo.YES.getValue());
        } else {
            int result = organizationDao.checkUserOrgIsCorrect(userId, organizationId);
            if (result < 1) {
                throw new MyException(500, "不存在此机构");
            }
            brandEntity.setOrganizationId(organizationId);
            brandEntity.setIsSystem(YesOrNo.NO.getValue());
        }

        Integer sort = bo.getSort();

        if (sort == null) {
            sort = 0;
        }

        brandEntity.setSort(sort);
        brandEntity.setCreatedBy(userId);
        brandEntity.setName(bo.getName().trim());
        brandEntity.setSlug(bo.getName().trim().toLowerCase());

        brandDao.insert(brandEntity);

    }

    @Override
    @Transactional
    public void updateBrand(boolean isAdmin, UpdateBrandBO bo, String userId, String organizationId) {
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", bo.getId());

        if (!isAdmin) {
            queryWrapper.eq("organization_id", organizationId);
            queryWrapper.eq("is_system", YesOrNo.YES.getValue());
        } else {
            queryWrapper.eq("is_system", YesOrNo.NO.getValue());
        }

        BrandEntity entity = brandDao.selectOne(queryWrapper);

        if (entity == null) {
            throw new MyException(404, "不存在的品牌");
        }

        Integer sort = bo.getSort();
        if (sort == null) {
            sort = 0;
        }

        entity.setSort(sort);
        entity.setName(bo.getName().trim());
        entity.setSlug(bo.getName().toLowerCase());

        brandDao.updateById(entity);

    }

    @Override
    public IPage<BrandEntity> getBrandList(Integer startPage, Integer pageSize, String name, boolean isSystem) {
        if (startPage == null || startPage <= 0) {
            startPage = 1;
        }

        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }

        Page<BrandEntity> page = new Page<>(startPage, pageSize);
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_system", isSystem ? YesOrNo.YES.getValue() : YesOrNo.NO.getValue());
        queryWrapper.orderByDesc("sort");
        if (!StringUtils.isBlank(name)) {
            queryWrapper.like("name", name);
        }


        return brandDao.selectPage(page, queryWrapper);
    }

    @Override
    public int getCountByOrzId(String brandId, String organizationId) {
        return brandDao.selectCount(
                new QueryWrapper<BrandEntity>()
                        .eq("id", brandId)
                        .eq("organization_id", organizationId)
        );
    }
}
